package com.mentaleye.biz.impl;

import com.mentaleye.bean.InterflowBean;
import com.mentaleye.biz.InterflowBiz;
import com.mentaleye.biz.StudentsBiz;
import com.mentaleye.dal.model.InterflowExample;
import com.mentaleye.dal.model.Students;
import com.mentaleye.service.InterflowService;
import com.mentaleye.util.CommonPage;
import com.mentaleye.util.CommonResult;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by Rock on 2017/5/12.
 */
@Service
public class InterflowBizImpl implements InterflowBiz {

    private static Logger logger = Logger.getLogger(InterflowBizImpl.class);

    @Autowired private InterflowService interflowService;
    @Autowired private StudentsBiz studentsBiz;

    public CommonResult edit(InterflowBean bean) {
        CommonResult result = new CommonResult();
        boolean success = false;
        try{
            if(bean.getInterflowId() != null){
                success = interflowService.edit(bean);
            }else{
                success = interflowService.add(bean);
            }
            result.setIsSuccess(success);
        }catch (Exception e){
            logger.error("编辑沟通记录异常",e);
        }
        return result;
    }

    public CommonResult removeList(List<Integer> ids){
        CommonResult result = new CommonResult();
        boolean success = false;
        try {
            InterflowExample example = new InterflowExample();
            example.createCriteria().andInterflowIdIn(ids);
            success = interflowService.removeByExample(example);
            result.setIsSuccess(success);
        }catch (Exception e){
            logger.error("批量删除沟通记录异常",e);
        }
        return result;
    }

    public InterflowBean query(Integer interflowId){
        try {
            InterflowBean bean = interflowService.query(interflowId);
            Students studentBean = studentsBiz.getStudentById(bean.getInterflowStudent());
            bean.setStuName(studentBean.getStuName());
            return bean;
        }catch (Exception e){
            logger.error("根据主键查询沟通记录异常",e);
        }
        return null;
    }

    public List<Map<String, Object>> queryNotStudent(){
        try {
            return interflowService.queryNotStudent();
        }catch (Exception e){
            logger.error("查询没有沟通记录的学生异常",e);
        }
        return null;
    }

    public void queryCommPage(CommonPage commonPage){
        try{
            interflowService.queryByCommpage(commonPage);
        }catch (Exception e){
            logger.error("分页查询沟通记录列表异常",e);
        }
    }
}
